Skip to content

[GEN][ZH] Fix compiler warnings for signed/unsigned mismatch of loop indices #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2025

Conversation

Mauller
Copy link

@Mauller Mauller commented May 3, 2025

This PR fixes loop index comparison warnings by matching the data type of the loop index to match the comparison values data type.

TODO:

  • Replicate in Generals.

@Mauller Mauller force-pushed the fix-loop-index-warnings branch from 14f7171 to 8aa4016 Compare May 3, 2025 16:32
@Mauller Mauller marked this pull request as ready for review May 3, 2025 16:39
@Mauller Mauller self-assigned this May 3, 2025
@Mauller Mauller added Minor Severity: Minor < Major < Critical < Blocker Build Anything related to building, compiling ZH Relates to Zero Hour labels May 3, 2025
@aliendroid1
Copy link

If you are going to change int to an unsigned type then I think std::size_t would be more appropriate and in line with standard conventions. Where range loops can be used then they should be preferred

Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fairly safe, but I suggest to give it a test for mismatch, especially with a focus on USA Countermeasures.

size_t is identical to std::size_t.

@@ -413,7 +413,7 @@ static void findHighFileNumber( AsciiString filename, void *userData )

// strip off the extension at the end of the filename
AsciiString nameOnly = filename;
for( Int count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ )
for( size_t count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this will call strlen on every iteration...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i think the VC6 version may do that but modern MSVC would likely compile it out as a constant.

@@ -579,7 +579,7 @@ Bool MapCache::loadUserMaps()
{
AsciiString endingStr;
AsciiString fname = s+1;
for (Int i=0; i<strlen(mapExtension); ++i)
for (size_t i=0; i<strlen(mapExtension); ++i)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another strlen in condition...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... i didn't want to change too much.

@@ -301,7 +301,7 @@ void CountermeasuresBehavior::launchVolley()
Object *obj = getObject();

Real volleySize = (Real)data->m_volleySize;
for( int i = 0; i < data->m_volleySize; i++ )
for( UnsignedInt i = 0; i < data->m_volleySize; i++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this will produce the same float value below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory it should as the raw binary values of positive signed int and unsigned int values are the same up to 2.7 billion at least.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes 34% of assembler in CountermeasuresBehaviour::launchVolley.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting how it changes that much when the index is only used in one place.

@Mauller Mauller force-pushed the fix-loop-index-warnings branch from 8aa4016 to f4c5b85 Compare May 4, 2025 07:26
@Mauller
Copy link
Author

Mauller commented May 4, 2025

Updated this with the inclusion of a cast that was missing in one of the loops.

@Mauller Mauller force-pushed the fix-loop-index-warnings branch from f4c5b85 to c0eff59 Compare May 4, 2025 16:40
@Mauller
Copy link
Author

Mauller commented May 4, 2025

Just rebased off recent main after the other warning fixes were merged.

@Mauller
Copy link
Author

Mauller commented May 4, 2025

Just ran Golden Replay 1 against this with the VC6 build and it did not mismatch.
Not sure if countermeasures were explicitly used by any of the USA players. But they were using every vehicles in the game during the test.

So I think the changes are fairly safe, i can replicate the changes to generals if people are happy.

Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unassemblize shows significant assembler changes in the following 4 functions. I suggest to focus mismatch test on functionality related to these.

ScriptActions::doMoveTeamTowardsNearest
DeliverPayloadNugget::create
W3DLaserDraw::W3DLaserDraw
CountermeasuresBehaviour::launchVolley

@@ -5296,7 +5296,7 @@ void ScriptActions::doMoveTeamTowardsNearest( const AsciiString& teamName, const
{
Real closestDist;
Real dist;
for( Int typeIndex = 0; typeIndex < objectTypes->getListSize(); typeIndex++ )
for( size_t typeIndex = 0; typeIndex < objectTypes->getListSize(); typeIndex++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a strange diff in ScriptActions::doMoveTeamTowardsNearest

image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i don't get that, if anything i would have through that most code changes would be reduction in code due to not needing to perform some casts.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks as if the function call has changed, but perhaps it is just an error in the tool.

@@ -322,7 +322,7 @@ class DeliverPayloadNugget : public ObjectCreationNugget
}

Object *firstTransport = NULL;
for( Int formationIndex = 0; formationIndex < m_formationSize; formationIndex++ )
for( UnsignedInt formationIndex = 0; formationIndex < m_formationSize; formationIndex++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This innocent looking edit changes 13% of the assembler inside DeliverPayloadNugget::create.

image

@@ -156,7 +156,7 @@ W3DLaserDraw::W3DLaserDraw( Thing *thing, const ModuleData* moduleData ) :
//Allocate an array of lines equal to the number of beams * segments
m_line3D = NEW SegmentedLineClass *[ data->m_numBeams * data->m_segments ];

for( int segment = 0; segment < data->m_segments; segment++ )
for( UnsignedInt segment = 0; segment < data->m_segments; segment++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes 55% of the assembler in W3DLaserDraw::W3DLaserDraw.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats interesting, you would think it would just be a few casts that got changed since segment is only used it one place within the function

@@ -301,7 +301,7 @@ void CountermeasuresBehavior::launchVolley()
Object *obj = getObject();

Real volleySize = (Real)data->m_volleySize;
for( int i = 0; i < data->m_volleySize; i++ )
for( UnsignedInt i = 0; i < data->m_volleySize; i++ )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes 34% of assembler in CountermeasuresBehaviour::launchVolley.

@xezon
Copy link

xezon commented May 15, 2025

This needs resolving, replicating, and a bit of testing.

@sorcerer86pt
Copy link

ok, tested vc6 with usa airforce gen. no problems with countermeasures.
usa air countermeasure vc6.zip

@Mauller
Copy link
Author

Mauller commented May 17, 2025

If people are happy i can replicate across to generals

@xezon xezon added this to the Code foundation build up milestone May 17, 2025
@xezon
Copy link

xezon commented May 18, 2025

Gogogo.

@Mauller Mauller force-pushed the fix-loop-index-warnings branch from c0eff59 to 19717e3 Compare May 18, 2025 18:59
@Mauller
Copy link
Author

Mauller commented May 18, 2025

Just a rebase and merge conflict fix with main first.

@Mauller Mauller force-pushed the fix-loop-index-warnings branch from 19717e3 to ff33644 Compare May 18, 2025 20:01
@Mauller Mauller changed the title [ZH] Fix loop index comparison warnings [GEN][ZH] Fix loop index comparison warnings May 18, 2025
@Mauller Mauller added the Gen Relates to Generals label May 18, 2025
@Mauller
Copy link
Author

Mauller commented May 18, 2025

Replicated to generals, some files and lines don't exist in the generals side in relation to zero hour.

@xezon xezon changed the title [GEN][ZH] Fix loop index comparison warnings [GEN][ZH] Fix compiler warnings for signed/unsigned mismatch of loop indices May 19, 2025
@xezon xezon merged commit 84f902f into TheSuperHackers:main May 19, 2025
21 checks passed
@xezon xezon deleted the fix-loop-index-warnings branch May 19, 2025 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Build Anything related to building, compiling Gen Relates to Generals Minor Severity: Minor < Major < Critical < Blocker ZH Relates to Zero Hour
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants